/*
Take the number 192 and multiply it by each of 1, 2, and 3:
192 × 1 = 192
192 × 2 = 384
192 × 3 = 576
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)
The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).
What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n &gt; 1?

Anser:932718654
Time:1.533628ms
*/
package main

import (
	"fmt"
	"time"
)

func main() {
	tstart := time.Now()
	max := 0
	for i := 1; i < 1e4; i++ {
		n := pandigital(i)
		if n > 1e8 {
			if isUnique(n) {
				if n > max {
					max = n
				}
			}
		}
	}
	fmt.Println(max)
	tend := time.Now()
	fmt.Println(tend.Sub(tstart))
}

// newNum 将b添加到a后面，形成新数
func newNum(a, b int) int {
	n := 10
	for b/n > 0 {
		n *= 10
	}
	return a*n + b
}

// isUnique 判断数字是否唯一
func isUnique(n int) bool {
	m := make(map[int]int)
	i := 0
	for n > 0 {
		i = n % 10
		if i == 0 {
			return false
		}
		m[i]++
		if m[i] > 1 {
			return false
		}
		n /= 10
	}
	return true
}

// pandigital 生成全数字
func pandigital(n int) int {
	p := n
	for i := 2; p < 1e8; i++ {
		p = newNum(p, n*i)
	}
	if p > 1e9 {
		return 0
	}
	return p
}
